package com.tiance.demo.service.mysql.analyzer.pageundolog;

import com.tiance.demo.service.mysql.bo.pageundolog.UndoLogSegmentHeader;
import com.tiance.demo.service.mysql.utils.ByteUtils;

public class UndoLogSegmentAnalyzer {

    public static UndoLogSegmentHeader analyse(byte[] bytes) {
        UndoLogSegmentHeader undoLogSegmentHeader = new UndoLogSegmentHeader();
        undoLogSegmentHeader.setBytes(bytes);

        analyseTrxUndoState(undoLogSegmentHeader);
        analyseTrxUndoLastLog(undoLogSegmentHeader);
        analyseTrxUndoFsegHeader(undoLogSegmentHeader);
        analyseTrxUndoPageList(undoLogSegmentHeader);

        return undoLogSegmentHeader;
    }

    private static void analyseTrxUndoState(UndoLogSegmentHeader undoLogSegmentHeader) {
        byte[] bytes = new byte[2];
        System.arraycopy(undoLogSegmentHeader.getBytes(), 0, bytes, 0, 2);

        int undoState = ByteUtils.bytes2Int(bytes);
        undoLogSegmentHeader.setTrxUndoState(undoState);
    }

    private static void analyseTrxUndoLastLog(UndoLogSegmentHeader undoLogSegmentHeader) {
        byte[] bytes = new byte[2];
        System.arraycopy(undoLogSegmentHeader.getBytes(), 0, bytes, 0, 2);

        int undoLastLog = ByteUtils.bytes2Int(bytes);
        undoLogSegmentHeader.setTrxUndoLastLog(undoLastLog);
    }

    private static void analyseTrxUndoFsegHeader(UndoLogSegmentHeader undoLogSegmentHeader) {
        byte[] bytes = new byte[2];
        System.arraycopy(undoLogSegmentHeader.getBytes(), 0, bytes, 0, 2);

        String fsegHeader = ByteUtils.bytes2HexStr(bytes);
        undoLogSegmentHeader.setTrxUndoFsegHeader(fsegHeader);
    }

    private static void analyseTrxUndoPageList(UndoLogSegmentHeader undoLogSegmentHeader) {
        byte[] bytes = new byte[2];
        System.arraycopy(undoLogSegmentHeader.getBytes(), 0, bytes, 0, 2);

        int undoPageList = ByteUtils.bytes2Int(bytes);
        undoLogSegmentHeader.setTrxUndoPageList(undoPageList);
    }
}
